﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;

namespace Portal.AssetTn.Model
{
    public class AssetStatusCls
    {
        public string AssetStatusId = "";
        public string AssetStatusName = "";
    }

    public class AssetStatusParser
    {
        public static AssetStatusCls CreateInstance()
        {
            //tao 1 class trang....
            AssetStatusCls oAssetStatus = new AssetStatusCls();
            return oAssetStatus;
        }

        public static AssetStatusCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetStatusCls oAssetStatus = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetStatus;
        }


        public static AssetStatusCls ParseFromDataRow(DataRow dr)
        {
            AssetStatusCls oAssetStatus = new AssetStatusCls();
            try
            {
                if (dr["AssetStatusId"] != DBNull.Value)
                {
                    oAssetStatus.AssetStatusId = (string)dr["AssetStatusId"];
                }
            }
            catch { }
            try
            {
                if (dr["AssetStatusName"] != DBNull.Value)
                {
                    oAssetStatus.AssetStatusName = (string)dr["AssetStatusName"];
                }
            }
            catch { }
           
            

            return oAssetStatus;
        }

        public static AssetStatusCls[] ParseFromDataTable(DataTable dtTable)
        {
            AssetStatusCls[] AssetStatuss = new AssetStatusCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetStatuss[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetStatuss;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("Asset");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetStatusId");
            ds.Tables["Info"].Columns.Add("AssetStatusName");
            return ds;
        }

        public static string GetXml(AssetStatusCls oAssetStatus)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetStatus.AssetStatusId,
                        oAssetStatus.AssetStatusName,
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetStatusCls oAssetStatus = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetStatus);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetStatusCls[] AssetStatuss = new AssetStatusCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetStatuss[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetStatuss);
            return Xml;
        }


        public static string GetXml(AssetStatusCls[] AssetStatuss)
        {
            if (AssetStatuss == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetStatuss = new AssetStatusCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetStatuss.Length; iIndex++)
            {
                AssetStatusCls oAssetStatus = AssetStatuss[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                         oAssetStatus.AssetStatusId,
                         oAssetStatus.AssetStatusName,
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetStatusCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetStatusCls[0];
            }
            AssetStatusCls[] AssetStatuss = new AssetStatusCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetStatuss[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetStatuss;
        }
    }
}
